import { createApp, h, provide } from 'vue';  // 直接使用根目录的 vue
import router from "./router";
import { setupStore } from "@/store";
import "@/assets/styles/root.scss";
import "./assets/styles/global.scss";
import "virtual:svg-icons-register";
import App from './App.vue';
import 'element-plus/dist/index.css';

import zhCn from "element-plus/es/locale/lang/zh-cn";
import ElementPlus from "element-plus";

import Adjust from "@/views/adjust/index.vue";
import Device from "@/views/deviceView/index.vue";
import Test from "@/views/testView/index.vue";
import Task from "@/views/taskView/index.vue";
import Setup from "@/views/setup/index.vue";
import User from "@/views/userView/index.vue";

const port = window.location.port;
console.log("port", port);
console.log("我要发版了啊666");

if (port === "3001") {

    const app = createApp(App);
    setupStore(app);

    app.use(router);

    app.use(ElementPlus, { locale: zhCn });

    app.mount('#app');
}

// 动态挂载方法（自动初始化 Pinia 和 Router）
/**
 * 动态挂载组件
 * @param el - 挂载目标元素或选择器
 * @param componentName - 要挂载的组件名称
 * @example
 * import myLib from 'my-lib';
 * myLib.mount('#app', 'Adjust');
 */
function mount(el: string | HTMLElement, componentName: string) {
    const component = getComponent(componentName)
    if (!component) {
        console.error(`[Plugin] Component "${componentName}" not found.`)
        return
    }

    const app = createApp({
        render: () => h(component),
    });

    provide("iconPrefix", "i");

    // 插件内部自行注册 Pinia 和 Router
    setupStore(app);
    // app.use(router);
    app.use(ElementPlus, { locale: zhCn })

    app.mount(el)
}

function getComponent(name: string) {
    const components = { Adjust, Device, Test, Task, Setup, User }
    return components[name as keyof typeof components]
}
export { Adjust, Device, Test, Task, Setup, User, mount }
export default { mount }
